package global

import (
	"flag"
	"gitee.com/cdsailyj/go-vpn/common/cfg"
	"gitee.com/cdsailyj/go-vpn/common/util"
	"github.com/spf13/viper"
	"os"
)

type Server struct {
	AppName     string    `mapstructure:"app-name" json:"app-name" yaml:"app-name"`
	GoHttpProxy string    `mapstructure:"go-http-proxy" json:"go-http-proxy" yaml:"go-http-proxy"`
	PubKey      string    `mapstructure:"pub-key" json:"pub-key" yaml:"pub-key"`
	XjytGateway string    `mapstructure:"xjyt-gateway" json:"xjyt-gateway" yaml:"xjyt-gateway"`
	VpnWsDomain string    `mapstructure:"vpn-ws-domain" json:"vpn-ws-domain" yaml:"vpn-ws-domain"`
	Redis       cfg.Redis `mapstructure:"redis" json:"redis" yaml:"redis"`
	// gorm
	Mysql     cfg.Mysql     `mapstructure:"mysql" json:"mysql" yaml:"mysql"`
	Pgsql     cfg.Pgsql     `mapstructure:"pgsql" json:"pgsql" yaml:"pgsql"`
	Xtk       cfg.Oracle    `mapstructure:"xtk" json:"xtk" yaml:"xtk"`
	Ywk       cfg.Oracle    `mapstructure:"ywk" json:"ywk" yaml:"ywk"`
	Lznlpt    cfg.Oracle    `mapstructure:"lznlpt" json:"lznlpt" yaml:"lznlpt"`
	Sqlite    cfg.Sqlite    `mapstructure:"sqlite" json:"sqlite" yaml:"sqlite"`
	AliyunOSS cfg.AliyunOSS `mapstructure:"aliyun-oss" json:"aliyun-oss" yaml:"aliyun-oss"`
}

type Setting struct {
	UserId    string `mapstructure:"user_id" json:"user_id" form:"user_id"`
	ServerUrl string `mapstructure:"server_url" json:"server_url" form:"server_url"`
	ProxyUrl  string `mapstructure:"proxy_url" json:"proxy_url" form:"proxy_url"`
}

var SettingApp *Setting

var (
	AppEnv = "dev"
	Cmd    = ""
	Query  = ""

	CONFIG Server
	Timer  = util.NewTimerTask()
	Viper  *viper.Viper
)

// ParseCmd  解析命令行
func ParseCmd() {
	var envInCmd string

	flag.StringVar(&envInCmd, "env", "", "环境变量")
	flag.StringVar(&Cmd, "c", "", "命令行方法")
	flag.StringVar(&Query, "q", "", "查询参数")
	// 解析命令行参数
	flag.Parse()

	//环境变量 命令行 > 环境变量 > 默认值
	env := os.Getenv("APP_ENV")
	if len(env) > 0 {
		AppEnv = env
	}
	if len(envInCmd) > 0 {
		AppEnv = envInCmd
	}
}
